package cn.lanqiao.business.service.impl;

import java.util.List;

import cn.lanqiao.business.domain.Student;
import cn.lanqiao.business.mapper.StudentMapper;
import cn.lanqiao.common.core.domain.entity.SysUser;
import cn.lanqiao.common.exception.ServiceException;
import cn.lanqiao.common.utils.DateUtils;
import cn.lanqiao.common.utils.SecurityUtils;
import cn.lanqiao.common.utils.StringUtils;
import cn.lanqiao.common.utils.bean.BeanValidators;
import cn.lanqiao.system.service.ISysDeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lanqiao.business.mapper.GradesMapper;
import cn.lanqiao.business.domain.Grades;
import cn.lanqiao.business.service.IGradesService;

import javax.validation.Validator;

/**
 * 学生成绩信息Service业务层处理
 * 
 * @author lmx
 * @date 2024-07-08
 */
@Service
public class GradesServiceImpl implements IGradesService 
{
    private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class);
    @Autowired
    private GradesMapper gradesMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询学生成绩信息
     * 
     * @param id 学生成绩信息主键
     * @return 学生成绩信息
     */
    @Override
    public Grades selectGradesById(Long id)
    {
        return gradesMapper.selectGradesById(id);
    }

    /**
     * 查询学生成绩信息列表
     * 
     * @param grades 学生成绩信息
     * @return 学生成绩信息
     */
    @Override
    public List<Grades> selectGradesList(Grades grades)
    {
        return gradesMapper.selectGradesList(grades);
    }

    /**
     * 新增学生成绩信息
     * 
     * @param grades 学生成绩信息
     * @return 结果
     */
    @Override
    public int insertGrades(Grades grades)
    {
        grades.setCreateTime(DateUtils.getNowDate());
        return gradesMapper.insertGrades(grades);
    }

    /**
     * 修改学生成绩信息
     * 
     * @param grades 学生成绩信息
     * @return 结果
     */
    @Override
    public int updateGrades(Grades grades)
    {
        grades.setUpdateTime(DateUtils.getNowDate());
        return gradesMapper.updateGrades(grades);
    }

    /**
     * 批量删除学生成绩信息
     * 
     * @param ids 需要删除的学生成绩信息主键
     * @return 结果
     */
    @Override
    public int deleteGradesByIds(Long[] ids)
    {
        return gradesMapper.deleteGradesByIds(ids);
    }

    /**
     * 删除学生成绩信息信息
     * 
     * @param id 学生成绩信息主键
     * @return 结果
     */
    @Override
    public int deleteGradesById(Long id)
    {
        return gradesMapper.deleteGradesById(id);
    }

    @Override
    public String importUser(List<Grades> gradesList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(gradesList) || gradesList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Grades user : gradesList) {
            //根据学生ID查询该学生是否存在
            Student stu = studentMapper.selectStudentByStuId(user.getStudentID());
            //根据学生ID查询该学生的学期成绩是否存在
            List<Grades> grades = gradesMapper.selectSemNameByStuId(user.getStudentID());
            //查询该课程对应的中间ID
            Grades grades1 = gradesMapper.selectCourseIdByCourseName(user.getCourseName());
            try {
                //判断该学生的id是否为空并且该学期没有成绩才能录入成功
                if (StringUtils.isNull(stu)){
                    BeanValidators.validateWithException(validator, user);
                    user.setCreateBy(operName);
                    user.setStuId(stu.getId());
                    user.setCourseId(grades1.getId());
                    gradesMapper.insertGrades(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生成绩 " + user.getStuName() + " 导入成功");
                }else if (grades.size() == 0){
                    BeanValidators.validateWithException(validator, user);
                    user.setCreateBy(operName);
                    user.setStuId(stu.getId());
                    user.setCourseId(grades1.getId());
                    gradesMapper.insertGrades(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生成绩 " + user.getStuName() + " 导入成功");
                }else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、该学生 " + user.getStuName() + "的成绩已存在");
                }
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "、学生成绩 " + user.getStuName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
            if (failureNum > 0)
            {
                failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                throw new ServiceException(failureMsg.toString());
            }
            else
            {
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            }
        }
        return successMsg.toString();
    }

    @Override
    public List<Grades> selectClassList() {
        return gradesMapper.selectClassList();
    }

    @Override
    public List<Grades> exportResults(String claName,String semName,String courseName) {
        return gradesMapper.exportResults(claName,semName,courseName);
    }

    @Override
    public Integer selectStuIdByStuName(String stuName) {
        return gradesMapper.selectStuIdByStuName(stuName);
    }


}
